home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / util / misc / ispell31.lha / ispell-3.1.18src / amiga.c next >
C/C++ Source or Header  |  1995-09-21  |  8KB  |  318 lines

  1. static char RCS_Amiga_ID[]=
  2.   "$Id: amiga.c 1.6 1995/09/21 15:39:06 jskov Exp $";
  3.  
  4. /*
  5.  * $Log: amiga.c $
  6.  * Revision 1.6  1995/09/21  15:39:06  jskov
  7.  * Now compiles with includes 3.1
  8.  *
  9.  * Revision 1.5  1995/07/03  23:54:58  jskov
  10.  * Removed #include "version.h" - version strings are now in an object
  11.  *
  12.  * Revision 1.4  1995/06/30  13:13:00  jskov
  13.  * RCS version string is now in the object code for easy id of patch revision
  14.  *
  15.  * Revision 1.3  1995/06/30  12:40:21  jskov
  16.  * Added initialization of ctoken
  17.  *
  18.  * Revision 1.2  1995/06/30  10:10:16  jskov
  19.  * Added "ACCEPT" command - accepts word for the rest of the session.
  20.  *
  21.  * Revision 1.1  1995/06/30  00:03:56  jskov
  22.  * Initial revision
  23.  *
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "config.h"
  29. #include "ispell.h"
  30. #include "proto.h"
  31. #include <dos/dos.h>
  32.  
  33. extern char * Version_ID[];
  34.  
  35. struct rexxCommandList rcl[] =
  36. {
  37.   {"add", &rexxadd},
  38.   {"quickadd", &rexxquickadd},
  39.   {"accept", &rexxaccept},
  40.   {"check", &rexxcheck},
  41.   {"quickcheck", &rexxquickcheck},
  42.   {"lookup", &rexxlookup},
  43.   {"filecheck", &rexxfilecheck},
  44.   {"version", &rexxversion},
  45.   {"exit", &rexxexit},
  46.   {NULL, NULL}
  47. };
  48.  
  49. SHORT KeepGoing = TRUE;    /* main loop control value */
  50.  
  51. char rootword[BUFSIZ];
  52.  
  53. int wflag = 0;
  54.  
  55. struct RexxMsg *RexxMsg;
  56.  
  57. void servermode()
  58. {
  59.   long rexxbit;
  60.   long returnbits;
  61.  
  62.   rexxbit = upRexxPort ("IRexxSpell", rcl, NULL, &disp);
  63.  
  64.   printf("Entered ARexx server mode!\n");
  65.   printf("Pass commands through port 'IRexxSpell'\n");
  66.   fflush (stdout);
  67.  
  68.   while (KeepGoing)
  69.     {
  70.       returnbits = Wait (rexxbit | SIGBREAKF_CTRL_C);
  71.       if (returnbits & SIGBREAKF_CTRL_C)
  72.     KeepGoing = 0;
  73.       if (returnbits & rexxbit)
  74.     dispRexxPort ();
  75.     }
  76. /*
  77.  *   With Rexx, we need to bring the port down.  You might make this
  78.  *   part of exit() for programs that have multiple paths to exit.
  79.  */
  80.   dnRexxPort ();
  81. }
  82.  
  83.  
  84. /*
  85.  *   Now we get into the actual code necessary for our REXX port; functions
  86.  *   that do the real work.  Note that this program was not structured
  87.  *   particularly nicely for Rexx; I had to write each of these functions.
  88.  *   Many programs have these subroutines already in place; they are called
  89.  *   as part of the event loop.  This progam, however, just has one big
  90.  *   switch statement with different actions . . .
  91.  *
  92.  *   First, our locals.
  93.  */
  94. /*
  95.  *   This is our main dispatch function.
  96.  */
  97. void disp (struct RexxMsg *msg, struct rexxCommandList *dat, char *p)
  98. {
  99.   (*(dat->userdata)) (msg, p);
  100. }
  101.  
  102. /*
  103.  *   This handler adds a word to the `global personal dictionary.'
  104.  *
  105.  *   Now, in English, the word is added to whatever personal
  106.  *   dictionary was found when ISpell was started up in ARexx
  107.  *   server mode. (Under a multiuser system, we might want to
  108.  *   restrict who can add words to this dictionary.  For now
  109.  *   we just do it!  Yes, the AmigaOS is multiuser, hehe :-).
  110.  *   No protection, the way true hackers like it, you (the 
  111.  *   user) get more rope to hang yourself :-), err the system 
  112.  *   with :-).
  113.  */
  114. void rexxadd (struct RexxMsg *msg, char *inword)
  115. {
  116.   if (*inword)
  117.     treeinsert (ichartosstr (strtosichar (inword, 0), 1), ICHARTOSSTR_SIZE, 1);
  118.   treeoutput ();
  119.   replyRexxCmd (msg, 0L, 0L, "ok");
  120. }
  121.  
  122. /*
  123.  *   The quick version does not write the changes to disk.
  124.  *   Useful if you will be adding many words.  Be sure to
  125.  *   call rexxadd at the end of the list (pass a null word,
  126.  *   or a valid new word to be added).
  127.  */
  128. void rexxquickadd (struct RexxMsg *msg, char *inword)
  129. {
  130.   if (*inword)
  131.     treeinsert (ichartosstr (strtosichar (inword, 0), 1), ICHARTOSSTR_SIZE, 1);
  132.   replyRexxCmd (msg, 0L, 0L, "ok");
  133. }
  134.  
  135. /*
  136.  * Accept word for the rest of this session. Obviously there is no need
  137.  * for saving the tree.
  138.  */
  139.  
  140. void rexxaccept (struct RexxMsg *msg, char *inword)
  141. {
  142.   if (*inword)
  143.     treeinsert (ichartosstr (strtosichar (inword, 0), 1), ICHARTOSSTR_SIZE, 0);
  144.   replyRexxCmd (msg, 0L, 0L, "ok");
  145. }
  146.  
  147. /*
  148.  *   This handler checks the spelling of a word.
  149.  */
  150.  
  151. void rexxcheck (struct RexxMsg *msg, char *inword)
  152. {
  153.   char buf[512];
  154.   char aWord[INPUTWORDLEN + MAXAFFIXLEN];
  155.  
  156.   int    i;
  157.  
  158.   for (i=0;i<=strlen(inword);i++){
  159.     ctoken[i]=inword[i];
  160.     itoken[i]=chartoichar(inword[i]);
  161.   }
  162.  
  163.   currentchar = inword;                        /* Global ISpell vars */
  164.  
  165.   if (good (itoken, 0, 0, 0, 0)){
  166.     if (hits[0].prefix == NULL && hits[0].suffix == NULL){
  167.       /* perfect match */
  168.       strcpy (buf, "*");
  169.     } else {
  170.       /* matched because of root */
  171.       sprintf (buf, "+ %s", hits[0].dictent->word);
  172.     }
  173.   } else if (compoundgood (itoken, 0)){
  174.     /* compound-word match */
  175.     strcpy (buf, "-");
  176.   }    else {
  177.     makepossibilities (itoken);
  178.     if (pcount){
  179.       /*
  180.       ** print &  or ?, ctoken, then
  181.       ** character offset, possibility
  182.       ** count, and the possibilities.
  183.       */
  184.       sprintf (buf, "%c %s %d", easypossibilities ? '&' : '?',ctoken, easypossibilities);
  185.       for (i = 0;  i < MAXPOSSIBLE;  i++){
  186.         if (possibilities[i][0] == 0)
  187.           break;
  188.         sprintf (aWord, "%c %s",i ? ',' : ':', possibilities[i]);
  189.       strcat(buf, aWord);
  190.       }
  191.     } else {
  192.       /*
  193.       ** No possibilities found for word TOKEN
  194.       */
  195.       (void) sprintf (buf, "# %s", ctoken);
  196.     }
  197.   }
  198.   replyRexxCmd (msg, 0L, 0L, buf);
  199. }
  200.  
  201.  
  202. /*
  203.  *   This handler checks the spelling of a word, it does
  204.  *   not try to find replacement words as above.  It only
  205.  *   determines whether or not the word is in the dictoinary.
  206.  */
  207. void rexxquickcheck (struct RexxMsg *msg, char *inword)
  208. {
  209.   int    i;
  210.  
  211.   for (i=0;i<=strlen(inword);i++)
  212.     itoken[i]=chartoichar(inword[i]);
  213.  
  214.   currentchar = inword;                        /* Global ISpell vars */
  215.  
  216.   if (good (itoken, 0, 0, 0, 0))
  217.     replyRexxCmd (msg, 0L, 0L, "ok");
  218.   else
  219.     replyRexxCmd (msg, 0L, 0L, "bad");
  220. }
  221.  
  222. /*
  223.  * Find words matching a regular expression
  224.  */
  225. void rexxlookup (struct RexxMsg *msg, char *p)
  226. {
  227.   char buf[512];
  228.   char cmd[512];
  229.   char *rval;
  230.   int whence = 0;
  231.   int bufend = 1;
  232.   int lookupsize;
  233.  
  234.   strcpy (buf, "&");
  235.   sprintf (cmd, "^%s$", p);
  236.   while ((rval = do_regex_lookup (cmd, whence)) != NULL){
  237.     whence=1;
  238.     lookupsize = strlen(rval);
  239.     if ((lookupsize + bufend) > 511)
  240.       break;
  241.     strcpy (&(buf[bufend++]), " ");
  242.     strcpy (&(buf[bufend]), rval);
  243.     bufend += lookupsize;
  244.   }
  245.   replyRexxCmd (msg, 0L, 0L, buf);
  246. }
  247.  
  248. /*
  249.  *   This handler checks the spelling of a file.
  250.  */
  251. void rexxfilecheck (struct RexxMsg *msg, char *p)
  252. {
  253.   char *cp;
  254.  
  255.   currentfile = p;
  256.  
  257.   if ((infile = fopen (p, "r")) == NULL){
  258.     replyRexxCmd (msg, 0L, 0L, "Error: Can't open input file");
  259.     return;
  260.   }
  261.  
  262.   tmpnam (tempfile);
  263.  
  264.   if ((outfile = fopen (tempfile, "w")) == NULL){
  265.     replyRexxCmd (msg, 0L, 0L, "Error: Can't open output file");
  266.     return;
  267.   }
  268.  
  269.   quit = 0;
  270.  
  271.   /* See if the file is a .tex file.  If so, set the appropriate flag. */
  272.   if ((cp = (char *) strrchr (p, '.')) != NULL && strcmp (cp, ".tex") == 0)
  273.     tflag = 1;
  274.   else
  275.     tflag = 0;
  276.  
  277.   lflag = 1;
  278.  
  279.   checkfile ();
  280.  
  281.   fclose (infile);
  282.   fclose (outfile);
  283.  
  284.   replyRexxCmd (msg, 0L, 0L, tempfile);
  285. }
  286.  
  287. /*
  288.  *   This handler returns the version of the program.
  289.  */
  290. void rexxversion (struct RexxMsg *msg, char *p)
  291. {
  292.   char buf[512];
  293.   int bufend;
  294.   int namesize;
  295.   int i;
  296.  
  297.   sprintf(buf, "%s\n%s\n", Version_ID[0], Version_ID[2]);
  298.   bufend=strlen(buf);
  299.   for (i = 0; rcl[i].name != NULL; i++){
  300.     namesize = strlen(rcl[i].name);
  301.     if ((namesize + bufend) > 511)
  302.       break;
  303.     bufend=+namesize+1;
  304.     strcat (buf, " ");
  305.     strcat (buf, rcl[i].name);
  306.   }
  307.   replyRexxCmd (msg, 0L, 0L, buf);
  308. }
  309.  
  310. /*
  311.  *   This handler sets the exit flag.
  312.  */
  313. void rexxexit (struct RexxMsg *msg, char *p)
  314. {
  315.   KeepGoing = 0;
  316.   replyRexxCmd (msg, 0L, 0L, "bye");
  317. }
  318.